package Readers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExperimentsReader {
	HSSFSheet sheet;
	HSSFWorkbook expWorkBook;
	File expFile;
	
	public ExperimentsReader(String expString) throws FileNotFoundException, IOException{
		String THIS_FOLDER = System.getProperty("user.dir")+"\\";
		expFile = new File(THIS_FOLDER+"\\"+expString);
		expWorkBook = new HSSFWorkbook(new FileInputStream(expFile));
		sheet = expWorkBook.getSheetAt(0);
	}
	
	/*Lihi's addition*/
	public String[] read_experiment_at_row(int rowNum) throws Exception{
		String [] args = new String[8];				
		args[0] = sheet.getRow(rowNum).getCell(0).getRichStringCellValue().getString();
		args[1] = sheet.getRow(rowNum).getCell(1).getRichStringCellValue().getString();
		args[2] = Double.toString(sheet.getRow(rowNum).getCell(2).getNumericCellValue() );
		args[3] = sheet.getRow(rowNum).getCell(3).getRichStringCellValue().getString();
		args[4] = Double.toString(sheet.getRow(rowNum).getCell(4).getNumericCellValue() );  
		args[5] = Double.toString(sheet.getRow(rowNum).getCell(5).getNumericCellValue() );
		args[6] = sheet.getRow(rowNum).getCell(6).getRichStringCellValue().getString();
		args[7] = sheet.getRow(rowNum).getCell(7).getRichStringCellValue().getString();		
		return args;		
	}
	
	public int getAmountOfExperiments(){
		return sheet.getLastRowNum();
	}
	
	public void write_to_expFile(int rowIdx, double avgQ, double stdev, long runtime) throws IOException{							
		HSSFRow row  = sheet.getRow(rowIdx);
		HSSFCell cell9   = row.createCell(9); 		
		cell9.setCellValue(avgQ);
		HSSFCell cell10   = row.createCell(10); 		
		cell10.setCellValue(stdev);
		HSSFCell cell11   = row.createCell(11); 		
		cell11.setCellValue(runtime);
		FileOutputStream fileOut = new FileOutputStream(expFile);
		expWorkBook.write(fileOut);
		fileOut.close();			 
	}

}
